/*********************************************
 * Copyright (c) 2010 ICBC.
 * All rights reserved.
 * Created on 2010-7-7 

 * Contributors:
 *     dccb-zhouh - initial implementation
 *********************************************/
package com.icbc.ctpdemo.user.opstep;

import java.io.BufferedOutputStream;
import java.io.File; 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;

import com.icbc.cte.base.TranFailException;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.annotations.Output;
import com.icbc.ctp.core.impl.OperationStep;
import com.icbc.ctp.ioc.xmlconfig.FileSystemXmlApplicationContext;

@Input(name = { "fileStream", "fileName" }, type = { "Blob", "String" }, description = {
		"", "" }, mustInput = { true, true }, isVariable = { true, true })
@Output(name = { "fullFilePath", "relativeFilePath" }, type = { "String",
		"String" }, description = { "", "" })
public class PicGenerateOpStep extends OperationStep {

	@Override
	protected int execute(IContext context) throws TranFailException {
		Blob blob = null;
		try {
			blob = (Blob) getInputValue(context, "fileStream");
		} catch (Exception e) {

		}

		if (blob == null) {
			setOutputValue(context, "fullFilePath", "");
			setOutputValue(context, "relativeFilePath", "");
			return 0;
		}

		String fileName = (String) getInputValue(context, "fileName");
		String filePath = FileSystemXmlApplicationContext.getFactory()
				.getPropertyConfigurer().getPros().getProperty("WEB_ROOT");

		File tempDir = null;
		tempDir = new File(filePath + "/" + "zoom");
		if (!tempDir.exists()) {
			tempDir.mkdir();
		}
		String tmpDir = tempDir.toString();

		FileOutputStream foStm = null;
		try {
			foStm = new FileOutputStream(new File(tmpDir + "/" + fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedOutputStream buff = new BufferedOutputStream(foStm);
		try {
			buff.write(blob.getBytes(1, (int) blob.length()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			buff.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			buff.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(tmpDir + "/" + fileName);
		setOutputValue(context, "fullFilePath", tmpDir + "/" + fileName);
		setOutputValue(context, "relativeFilePath", "zoom" + "/" + fileName);
		/*
		 * InputStream in = null; try { in = blob.getBinaryStream(); } catch
		 * (SQLException e) { e.printStackTrace(); } byte[] P_Buf = new byte[10
		 * * 1024 * 1024]; int byteRead = 0; try { while ((byteRead =
		 * in.read(P_Buf)) != -1) { foStm.write(P_Buf, 0, byteRead); } } catch
		 * (IOException e) { e.printStackTrace(); } try { in.close();
		 * foStm.flush(); } catch (IOException e) { e.printStackTrace(); }
		 */

		return 0;
	}

}
